//
//  CommendTable.m
//  XRmenu-HD
//
//  Created by shengchao yang on 11-12-10.
//  Copyright (c) 2011年 home user. All rights reserved.
//

#import "CommendTable.h"
#import "CommendTableCell.h"
@interface CommendTable(Private) 
-(void) MyOrderNumberChanged;
@end

@implementation CommendTable

-(id) initWithStyle:(NSString *)stylename ViewName:(NSString *)vname
{
    self = [super initWithStyle:stylename DishSubViewName:vname];
    if (self) {  
        NSDictionary *tdict = [[[[APPSESSION.allconfig objectForKey:@"UI Skin"] objectForKey:stylename] objectForKey:@"Commend View"] objectForKey:@"CommendTable View"];
        NSDictionary *tabdict = [[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"TableView CSS"] objectForKey:[tdict getStringValueAtPath:@"sub tableview/table view style"]];
        if (tdict) {
            [self setTableViewProperty:tabdict Frame:CGRectFromString([tdict getStringValueAtPath:@"sub tableview/table view rect"])];
        }
        
        addNObserver(@selector(MyOrderNumberChanged), kMessage_CommendTable_MyOrderNumberChanged);
        postN(kMessage_CommendTable_MyOrderNumberChanged);//更新已点份数
        [self viewWithTag:kTagOfStartBT+3].hidden=YES;
    }
    return self;
}
-(void) MyOrderNumberChanged{
    [APPSESSION getAllMyOrder];
    //显示已点份数
    if ([self viewWithTag:kTagOfStartBT]) {
        UIButton *btn =(UIButton *) [self viewWithTag:kTagOfStartBT]; 
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:@"myorder_number.png"]] forState:UIControlStateNormal];
        [btn setTitle:[NSString stringWithFormat:@"%d",[APPSESSION.myOrders count]] forState:UIControlStateNormal];
    }
}

#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender
{
	NSInteger bid = [sender tag];
    if(bid == (kTagOfStartBT + 1)){
        DebugLog(@"%@", @"显示我的点单界面");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        postN(kMessage_RootView_MyOrderViewDisplay);
    }
	else if (bid == (kTagOfStartBT + 2)) {
        if (APPSESSION.isAnimating) {
            DebugLog(@"动画还未结束...");
            return;
        }
        DebugLog(@"进入点菜界面");
        APPSESSION.isViewRemoveFromLef=YES;
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        removeNObserver(kMessage_CommendTable_MyOrderNumberChanged);
        NSDictionary *infos1 = [NSDictionary dictionaryWithObjectsAndKeys:num(ViewOfProduct), @"Step", nil];
        postNWithInfos(kMessage_RootView_ChangeStep, self, infos1);
	}
}
#pragma mark -
#pragma mark Tableview delegate and datasource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [APPSESSION.allCommendPlu count];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LanguageCell";
	CommendTableCell *cell = (CommendTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		NSString *c = [APPSESSION.allconfig getStringValueAtPath:[NSString stringWithFormat:@"UI Skin/%@/Commend View/CommendTable View/sub tableview/table cell style", APPSESSION.uistyle]];
		cell = (CommendTableCell *)[[[CommendTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier Customer:c] autorelease];
	}
    else{
    }
	[self configureCell:cell atIndexPath:indexPath];	
    return cell;	
}
- (void)configureCell:(CommendTableCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{
	PluPlu *item = [APPSESSION.allCommendPlu objectAtIndex:indexPath.row];
    cell.celldata = item;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (APPSESSION.isAnimating) {
        DebugLog(@"动画还未结束...");
        return;
    }
    APPSESSION.isViewRemoveFromLef=YES;
	[aTableView deselectRowAtIndexPath:indexPath animated:NO];// don't keep the table selection
    
    PluPlu *selectedPlu = [APPSESSION.allCommendPlu objectAtIndex:indexPath.row];
    PluSort *selectedSort = selectedPlu.rsToPluSort;
    PluMenu *selectedMenu = selectedSort.rsToPluMenu;
    DebugLog(@"selectMenuCode=%@,selectSortCode=%@,selectPluCode=%@",
             selectedMenu.pmCode,
             selectedSort.psPluSortId,
             selectedPlu.ppPluPluId);
    DebugLog(@"selectMenuName=%@,selectSortName=%@,selectPluName=%@",
             JsonFiled(selectedMenu.pmDescripts),
             JsonFiled(selectedSort.psDescripts),
             JsonFiled(selectedPlu.ppNames));
    
    
    for (int i=0; i<[APPSESSION.pluMenus count]; i++) {
        PluMenu *tempMenu=(PluMenu *)[APPSESSION.pluMenus objectAtIndex:i];
        if ([tempMenu.pmCode isEqualToString:selectedMenu.pmCode]) {
            DebugLog(@"menuIndex=%d", i);
            if (i!=APPSESSION.currentMenuIndex) {
                [APPSESSION updateCurrentMenuIndex:i];
            }
            break;
        }
    }
    
    for (int i=0; i<[APPSESSION.pluSorts count]; i++) {
        PluSort *tempSort=(PluSort *)[APPSESSION.pluSorts objectAtIndex:i];
        if ([tempSort.psPluSortId isEqualToString:selectedSort.psPluSortId]) {
            DebugLog(@"sortIndex=%d", i);
            if (i!=APPSESSION.currentSortIndex) {
                [APPSESSION updateCurrentSortIndex:i];
            }
            break;
        }
    }
    
    for (int i=0; i<[APPSESSION.pluPlus count]; i++) {
        PluPlu *tempPlu=(PluPlu *)[APPSESSION.pluPlus objectAtIndex:i];
        if ([tempPlu.ppPluPluId isEqualToString:selectedPlu.ppPluPluId]) {
            DebugLog(@"pluIndex=%d", i);
            if (i!=APPSESSION.currentPluIndex) {
                [APPSESSION updateCurrentPluIndex:i];
            }
            break;
        }
    }
    
    NSDictionary *infos1 = [NSDictionary dictionaryWithObjectsAndKeys:num(ViewOfProduct), @"Step", nil];
    postNWithInfos(kMessage_RootView_ChangeStep, self, infos1);
}
- (void)tableView:(UITableView *)aTableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    
    NSIndexPath *lastIndexPath = [[aTableView indexPathsForVisibleRows] lastObject];    
    if (lastIndexPath.row<([APPSESSION.allCommendPlu count]-1)) {
        [self viewWithTag:kTagOfStartBT+3].hidden=NO;
    }
    else{
        [self viewWithTag:kTagOfStartBT+3].hidden=YES;
    }
}
//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    removeNObserver(kMessage_CommendTable_MyOrderNumberChanged);
    [super dealloc];
}
@end
